knitr::opts_chunk$set(echo = T, warning = F, message = F, fig.path = 'figures/')

library(tbeptools)
library(tidyverse)
library(lubridate)
library(plotly)
library(htmltools)

data(trnpts)
data(tbsegshed)
data(otbtrn)

otbshed <- tbsegshed %>% 
  filter(bay_segment %in% 'OTB')
otbpts <- trnpts[otbshed, ]

##
# get spp freq occurrence

covest <- otbtrn %>% 
  group_by(Date, Transect, Savspecies) %>% 
  nest() %>% 
  mutate(
    est = purrr::map(data, function(data){
      
      foest <- sum(data$bb > 0, na.rm = T) / nrow(data)   
      bbest <- sum(data$bb, na.rm = T) / nrow(data)
      out <- tibble(foest = foest, bbest = bbest)
      
      return(out)
      
    })
  ) %>% 
  select(-data) %>% 
  unnest(est) %>%
  ungroup %>% 
  filter(Savspecies != 'No Cover') %>% 
  mutate(
    Savspecies = factor(Savspecies, levels = rev(c('AA', 'DA', 'Halodule', 'Thalassia', 'Syringodium', 'Ruppia', 'Halophila spp.')))
  )
bbchg <- covest %>% 
  group_by(Transect) %>% 
  nest() %>%
  mutate(
    pout = purrr::pmap(list(Transect, data), function(Transect, data){

      plot_ly(data, x = ~as.character(Date), y = ~bbest, color = ~Savspecies, stackgroup = 'one', type = 'scatter', mode = 'none') %>%
        layout(
          title = Transect,
          yaxis = list(title = 'Mean Braun-Blanquet scores'), #, gridcolor = '#FFFFFF'),
          xaxis = list(title = NA, gridcolor = '#FFFFFF', tickfont = list(size = 8), tickangle = 45),
          barmode = 'stack',
          showlegend = T
        )
    
      # out <- ggplot() + ggtitle(Transect)
      
      # return(out)
       
    })
  ) %>% 
  select(-data) %>% 
  deframe()

Abundance changes over time

S1T13

bbchg[["S1T13"]]

S1T13

bbchg[["S1T13"]]

S1T1

bbchg[["S1T1"]]

S1T3

bbchg[["S1T3"]]

S1T17

bbchg[["S1T17"]]

S1T16

bbchg[["S1T16"]]

S1T18

bbchg[["S1T18"]]

S1T14

bbchg[["S1T14"]]

S1T8

bbchg[["S1T18"]]

S1T15

bbchg[["S1T5"]]

S3T10

bbchg[["S3T10"]]

S1T6

bbchg[["S1T6"]]

S1T15

bbchg[["S1T15"]]
# cat('## Abundance changes over time {.tabset .tabset-pills}')
# create our top-level div for our tabs
tags$div(
  # create the tabs with titles as a ul with li/a
  tags$ul(
    class="nav nav-tabs",
    role="tablist",
    lapply(
      names(bbchg),
      function(p){
        tags$li(
          tags$a(
            "data-toggle"="tab",
            href=paste0("#tab-",p),
            p
          )
        )
      }
    )
  ),
  # fill the tabs with our plotly plots
  tags$div(
    class="tab-content",
    lapply(
      names(bbchg),
      function(p){
        tags$div(
          #  make the first tabpane active
          class=ifelse(p==names(bbchg)[1],"tab-pane active","tab-pane"),
          #  id will need to match the id provided to the a href above
          id=paste0("tab-",p),
          as.widget(bbchg[[p]])
        )
      }
    )
  )
  ) %>%
  # attach the necessary dependencies
  #  since we are manually doing what rmarkdown magically does for us
  attachDependencies(
    list(
      rmarkdown::html_dependency_jquery(),
      shiny::bootstrapLib()
    )
  )